#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>

int main ()
{
    int handle = socket (AF_INET, SOCK_DGRAM, 0);

    struct sockaddr_in local_addr;
    memset (&local_addr, 0, sizeof (local_addr));

    local_addr.sin_family = AF_INET;
    local_addr.sin_port   = htons (8881);
    local_addr.sin_addr.s_addr   = htonl(INADDR_ANY);
    bind (handle, (struct sockaddr *)&local_addr, sizeof (struct sockaddr));
    perror ("bind");

    printf ("l = %d, n = %d\n", sizeof (struct sockaddr), sizeof (struct sockaddr_in));
    char buff[256] = {0};
    while (1)
    {
        struct sockaddr_in remote_addr;
        memset (&remote_addr, 0, sizeof (remote_addr));
        int len = sizeof (struct sockaddr);
        int result = recvfrom (handle, buff, sizeof (buff) - 1, 0, 
                (struct sockaddr *)&remote_addr,
                (socklen_t *)&len);
        printf ("recv %s:%d-> %s\n", inet_ntoa (remote_addr.sin_addr), 
                ntohs (remote_addr.sin_port),
                buff);
        sendto (handle, buff, result, 0, 
                (struct sockaddr *)&remote_addr,
                len);
        printf ("send %s:%d-> %s\n", inet_ntoa (remote_addr.sin_addr), 
                ntohs (remote_addr.sin_port),
                buff);
        memset (buff, 0, sizeof (buff));
    }
    close (handle);
    return 0;
}
